<?php 
	class CategoryController extends PlatformController{
		public function indexAction(){

			$category=Factory::M('CategoryModel');
			$cateInfo=$category->getCategory();
			$this->assign('cateInfo',$cateInfo);
			$this->display('index.html');
		}
		public function addAction(){
			
			$category=Factory::M('CategoryModel');
			$cateInfo=$category->getCategory();
			$this->assign('cateInfo',$cateInfo);
			$this->display('add.html');

		}
		function dealAddAction(){
			$cate=array();
			$cate['cate_name']=$this->escapeData($_POST['cate_name']);
			$cate['cate_pid']=$_POST['cate_pid'];
			$cate['cate_sort']=$this->escapeData($_POST['cate_sort']);
			$cate['cate_desc']=$this->escapeData($_POST['cate_desc']);
			if(empty($cate['cate_name'])||empty($cate['cate_desc'])||empty($cate['cate_sort'])){
				$this->jump('index.php?p=Back&c=Category&a=add','信息不完整');
			}
			if(!is_numeric($cate['cate_sort'])||(int)$cate['cate_sort']!=$cate['cate_sort']||$cate['cate_sort']<1){
				$this->jump('index.php?p=Back&c=Category&a=add','排序应该为1到50');
			}
			$category=Factory::M('CategoryModel');
			$result=$category->insertCate($cate);
			if($result){
				$this->jump('index.php?p=Back&c=Category&a=index');

			}else{
				$this->jump('index.php?p=Back&c=Category&a=add','发生未知错误,添加失败');
			}
		}
		function editAction(){
			$cate_id=$_GET['cate_id'];

			$category=Factory::M('CategoryModel');
			$cate=$category->getCategoryById($cate_id);
			$this->assign('cate',$cate);
			$cateInfo=$category->getCategory();
			$this->assign('cateInfo',$cateInfo);
			$this->display('edit.html');
		}
		public function dealEditAction(){
			$cate=array();
			$cate['cate_name']=$this->escapeData($_POST['cate_name']);
			$cate['cate_pid']=$_POST['cate_pid'];
			$cate['cate_sort']=$this->escapeData($_POST['cate_sort']);
			$cate['cate_desc']=$this->escapeData($_POST['cate_desc']);
			$cate['cate_id']=$_POST['cate_id'];
			if(empty($cate['cate_name'])||empty($cate['cate_desc'])||empty($cate['cate_sort'])){
				$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}",'信息不完整');
			}
			if(!is_numeric($cate['cate_sort'])||(int)$cate['cate_sort']!=$cate['cate_sort']||$cate['cate_sort']<1){
				$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}",'排序应该为1到50');
			}
			$category=Factory::M('CategoryModel');
			$result=$category->updatecateById($cate);
			if($result){
				$this->jump('index.php?p=Back&c=Category&a=index');
			}else{
				$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}",'发生未知错误!');
			}
		}
		function delAction(){
			$cate_id=$_GET['cate_id'];
			$category=Factory::M('CategoryModel');

			$subId=$category->getSubId($cate_id);

			if($subId){
				$this->jump('index.php?p=Back&c=Category&a=index','下面还有小弟,不能删除');
			}
			$result=$category->delCategoryById($cate_id);
			if($result){
				$this->jump('index.php?p=Back&c=Category&a=index');
			}
			else{
				$this->jump('index.php?p=Back&c=Category&a=index','发生未知错误');
		}
		}
		function delAllAction(){
			if(!isset($_POST['cate_id'])){
				$this->jump("index.php?p=Back&c=Category&a=index",'请先选择要删除的类');
			}
			$cate_id=$_POST['cate_id'];
			$category=Factory::M('CategoryModel');
			foreach($cate_id as $id){
				if($category->getSubId($id)){
					$this->jump('index.php?p=Back&c=Category&a=index','下面还有小弟,不能删除');
				}
			}
			$result=$category->delAllCategory($cate_id);
			if($result){
				$this->jump('index.php?p=Back&c=Category&a=index');
			}
			else{
				$this->jump('index.php?p=Back&c=Category&a=index','发生未知错误');
		}
		}

	}





 ?>